package com.yx.cloud.base;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.iot.common.utils.Event;
import com.iot.common.utils.LogUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.sweeper.AppContext;
import com.yx.cloud.AppStatusConstant;
import com.yx.cloud.AppStatusManager;
import com.yx.cloud.MyApplication;
import com.yx.cloud.R;
import com.yx.cloud.activity.WelcomeUI;
import com.yx.cloud.receiver.GlobalBroadcastReceiver;
import com.yx.cloud.utils.AtyContainer;
import com.yx.cloud.utils.LanguageUtils;
import com.yx.cloud.utils.StatusBarColorUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class BaseActivity extends AppCompatActivity {
    private final static String TAG = "BaseActivity";

    private boolean connected;
    private boolean factory_reset;
    private boolean saveOnLineState = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 添加Activity到堆栈
        AtyContainer.getInstance().addActivity(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        StatusBarColorUtils.setStatusBarColor(this, R.color.white, "0");
        LanguageUtils.getAndsetLanguage(this, "base");

        int status = AppStatusManager.getInstance().getAppStatus();
        LogUtils.i("getAppStatus" + AppStatusManager.getInstance().getAppStatus());
        if (status == AppStatusConstant.STATUS_FORCE_KILLED) {
            //如果应用被后台杀掉 重新走welcomeUI
            Intent intent = new Intent(this, WelcomeUI.class);
            startActivity(intent);
            finish();
        } else {
            //其实还有很多状态 这里只考虑 AppStatusConstant.STATUS_NORMAL
            onCreate();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AtyContainer.getInstance().removeActivity(this);
    }

    protected void onCreate() {
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        if (MyApplication.receiver == null) {
            MyApplication.receiver = new GlobalBroadcastReceiver();
            registerReceiver(MyApplication.receiver, "com.yx.cloud.device.state");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        //LogUtils.i("device event : " + event.toString());
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_CONNECTED:
                //应用内监控机器是否在线离线状态
                LogUtils.i("device event 1 : " + event.isResult());
                if (event.isResult()) {
                    //全局监听在线离线状态，但是要在连接上设备时
                    Robot robot = AppContext.get().getCurrentDevice();
                    connected = (boolean) event.getContent();
                    LogUtils.i("device event 2 : " + MyApplication.recvActive);
                    if (robot != null && MyApplication.recvActive) {
                         LogUtils.i(saveOnLineState + " , " + connected + "fffffff" + (("" + saveOnLineState).equals((connected + ""))));
                        if (!("" + saveOnLineState).equals((connected + ""))) {
                            saveOnLineState = connected;
                            sendDeviceState(connected);
                        }
                    }
                }
                break;
            case Event.MESSAGE_TYPE_DEVICE_RESET:
                //应用内监控机器是否重置
                //LogUtils.i("重置 广播了 回调 ");
                if (event.isResult()) {
                    //LogUtils.i("重置 广播了 回调 1111111");
                    //全局监听在线离线状态，但是要在连接上设备时
                    Robot robot = AppContext.get().getCurrentDevice();
                    if (robot != null && MyApplication.recvActive) {
                        factory_reset = (boolean) event.getContent();
                        // LogUtils.i("重置 广播了 回调 3333333333 " + factory_reset);
                        sendDeviceReset(factory_reset);
                    }
                }
                break;
        }
    }

    private void registerReceiver(BroadcastReceiver bc, String action) {
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(action);
        registerReceiver(bc, mFilter);
        LogUtils.i("注册 广播了。。。。。");
    }

    private void sendDeviceState(boolean online) {
        //上线、离线广播发送
        Intent intent = new Intent();
        intent.setAction("com.yx.cloud.device.state");
        intent.putExtra("tag", "online");
        intent.putExtra("connected", online);
        sendBroadcast(intent);
         LogUtils.i("发送 广播了。。。。。");
    }

    private void sendDeviceReset(boolean factory_reset) {
        //重置设备广播发送
        Intent intent = new Intent();
        intent.setAction("com.yx.cloud.device.state");
        intent.putExtra("tag", "factory_reset");
        intent.putExtra("sendDeviceReset", factory_reset);
        sendBroadcast(intent);
        LogUtils.i("重置 广播了 发送了");
    }
}
